#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@File: 			client.py
@Version: 		1.0
@Author: 		kztxt
@Email: 		kztxt@163.com
@CreatedAt: 	2025-09-30 14:59:28
'''

# here put the import lib
import sys, os
import requests
from pydub import AudioSegment

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append('{}/cosyvoice2'.format(ROOT_DIR))
sys.path.append('{}/cosyvoice2/Matcha-TTS'.format(ROOT_DIR))

def client(text, mp3_path):
    prompt_sr, target_sr = 16000, 22050
    url = 'http://127.0.0.1:19092/to_speech/inference_zero_shot'
    payload = {
        'tts_text': text, 
        'instruct_text':'', 
        'prompt_text':'She won the championship for three consecutive years. They worked for five consecutive days to complete the project. The report outlines consecutive steps to improve efficiency. Please number the pages in consecutive.'
    }
    files = [('prompt_wav', ('prompt_wav', open('sample/音频样例-英文-男.wav', 'rb')))]
    response = requests.post(url, data=payload, files=files, stream=True)
   
    tts_audio = response.content
    output_wav = 'output/tts_audio.mp3'
    with open(output_wav, 'wb') as f:
        f.write(tts_audio)
    #AudioSegment.from_mp3(tts_audio).export(mp3_path, format='mp3')

def client_male(text, mp3_path):
    prompt_sr, target_sr = 16000, 22050
    # url = 'http://127.0.0.1:19092/to_speech/inference_zero_shot'
    url = 'http://121.18.111.226/to_speech/inference_zero_shot'
    payload = {
        'tts_text': text, 
        'instruct_text':'', 
        'prompt_text':'She won the championship for three consecutive years. They worked for five consecutive days to complete the project. The report outlines consecutive steps to improve efficiency. Please number the pages in consecutive.'
    }
    files = [('prompt_wav', ('prompt_wav', open('sample/音频样例-英文-男.wav', 'rb')))]
    response = requests.post(url, data=payload, files=files, stream=True)
   
    tts_audio = response.content
    output_wav = 'output/tts_audio.mp3'
    with open(output_wav, 'wb') as f:
        f.write(tts_audio)
    AudioSegment.from_mp3(output_wav).export(mp3_path, format='mp3')

def client_female(text, mp3_path):
    prompt_sr, target_sr = 16000, 22050
    url = 'http://127.0.0.1:19092/to_speech/inference_zero_shot'
    # url = 'http://121.18.111.226/to_speech/inference_zero_shot'
    payload = {
        'tts_text': text, 
        'instruct_text':'声音高清, 发音精准, 具有播音员的声音品质', 
        'prompt_text':'She won the championship for three consecutive years. They worked for five consecutive days to complete the project. The report outlines consecutive steps to improve efficiency. Please number the pages in consecutive order.'
    }
    files = [('prompt_wav', ('prompt_wav', open('sample/音频样例-英文-女.wav', 'rb')))]
    response = requests.post(url, data=payload, files=files, stream=True)
   
    tts_audio = response.content
    output_wav = 'output/tts_audio.mp3'
    with open(output_wav, 'wb') as f:
        f.write(tts_audio)
    AudioSegment.from_mp3(output_wav).export(mp3_path, format='mp3')
    
def merge_audio(mp3_path1, mp3_path2, output_mp3_path):
    audio1 = AudioSegment.from_mp3(mp3_path1)
    audio_delay = AudioSegment.silent(duration=200)
    audio2 = AudioSegment.from_mp3(mp3_path2)
    audio = audio1 + audio_delay + audio2
    audio.export(output_mp3_path, format='mp3')

if __name__ == '__main__':
    client_female("add", "output/125-0-male.mp3")
    # client_male("around 700 million metric tons a drop of around 3 percent in energy consumption per unit of GDP continued improvements in the environment", "output/125-2-male.mp3")
    # merge_audio("output/125-1-male.mp3", "output/125-2-male.mp3", "output/125-male.mp3")
    #client("around 700 million metric tons a drop of around 3 percent in energy consumption per unit of GDP continued improvements in the environment", "output/output.mp3")